﻿//Description:MES推送给工控网
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:Wangjh
//create Date:2022-11-1
//Rewriter:
//Rewrite Date:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using log4net;
using SendIndustrialNetwork.Service;
//using UFC.Common; 

namespace SendIndustrialNetwork
{
    public partial class SendIndustrialNetwork : ServiceBase
    {
        public SendIndustrialNetwork()
        {
            InitializeComponent();
        }

        private System.Timers.Timer m_SyncTimer;
        private System.Timers.Timer m_ThreeCoordinateTimer;//与三坐标系统集成
        private System.Timers.Timer m_RBResCollectionTimer;//与热表设备采集系统集成
        private System.Timers.Timer m_MesToDcTimer;//与数据中心（表结构、表数据、工艺附件）集成
        private System.Timers.Timer m_RBMesToDcTimer;//与数据中心（热表主数据）add by zhangrj 20250725
        private System.Timers.Timer m_SaveRbShipMesDataTimer;//热表转工子卡可推送数据写入变更表（热表） add by zhangrj 20250711
        private System.Timers.Timer m_SaveRbOrderMesDataTimer;//热表采购订单卡可推送数据写入变更表（热表）add by zhangrj 20250711
        private System.Timers.Timer m_SaveRbOtherMesDataTimer;//热表其他表数据可推送数据写入变更表（热表）add by zhangrj 20250711
        private System.Timers.Timer m_SaveMesContainerDataTimer;//将【件号】流卡可推送数据写入变更表
        private System.Timers.Timer m_SaveMesWorkflowStepDataTimer;//将排产下发工序级任务可推送数据写入变更表
        private System.Timers.Timer m_SaveMesJxWorkflowStepDataTimer;//将【机匣Json】排产下发工序级任务可推送数据写入变更表
        private System.Timers.Timer m_DealJxReportAndStatusDataTimer;//处理【机匣Json】工序报工和状态数据 add by longb 2025-01-02
        private ILog rsLog = LogHelp.receiveSave; //接收处理程序专用日志 add:wangjh 20241111
        private System.Timers.Timer m_ReceiveSaveBusinessIDTimer;//处理接收到的businessid add:wangjh 20241111
        private System.Timers.Timer m_SendMESDataToWMSTimer;//MES->WMS add zhunan 2025.9.12
        private System.Timers.Timer m_SaveContainerMaterialTimer;//处理WMS推送给MES的发料信息json add zhunan 2025.9.19
        private System.Timers.Timer m_SaveDealFaultBillTimer;//处理WMS推送给MES的返件领取信息json add zhunan 2025.9.22
        private System.Timers.Timer m_DealWMSwayBillFeedBackTimer;//处理WMS推送给MES的物流信息回传json add longb 20250923
        private System.Timers.Timer m_FiveDealFaultBillTimer;//处理五厂排故返件回传json add longb 20251103

        protected override void OnStart(string[] args)
        {
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());
            LogHelp.SetConfig();
            LogHelp.WriteLog("定时器启动");
            //SyncTimerStatr(ref m_SyncTimer, m_SyncTimer_Elapsed, intSync * 10 * 1000);
            //SyncTimerStatr(ref m_RBResCollectionTimer, m_RBResCollectionTimer_Elapsed, intSync * 10 * 1000);//与热表设备集成

            //SyncTimerStatr(ref m_ThreeCoordinateTimer, m_ThreeCoordinateTimer_Elapsed, intSync * 10 * 1000);//与三坐标集成
            //SyncTimerStatr(ref m_MesToDcTimer, m_MesToDcTimer_Elapsed, intSync * 10 * 1000);//与数据中心集成
            //SyncTimerStatr(ref m_RBMesToDcTimer, m_RBMesToDcTimer_Elapsed, intSync * 10 * 1000);//与数据中心集成（热表主数据）
            //SyncTimerStatr(ref m_SaveRbShipMesDataTimer, m_SaveRbShipMesDataTimer_Elapsed, intSync * 10 * 1000);//热表转工子卡可推送数据写入变更表（热表） add by zhangrj 20250711
            //SyncTimerStatr(ref m_SaveRbOrderMesDataTimer, m_SaveRbOrderMesDataTimer_Elapsed, intSync * 10 * 1000);//热表采购订单卡可推送数据写入变更表（热表） add by zhangrj 20250711
            //SyncTimerStatr(ref m_SaveRbOtherMesDataTimer, m_SaveRbOtherMesDataTimer_Elapsed, intSync * 10 * 1000);//热表其他表数据可推送数据写入变更表（热表） add by zhangrj 20250711
            //SyncTimerStatr(ref m_SaveMesContainerDataTimer, m_SaveMesContainerDataTimer_Elapsed, intSync * 10 * 1000);//将【件号】流卡可推送数据写入变更表
            //SyncTimerStatr(ref m_SaveMesWorkflowStepDataTimer, m_SaveMesWorkflowStepDataTimer_Elapsed, intSync * 10 * 1000);//将排产下发工序级任务可推送数据写入变更表
            //SyncTimerStatr(ref m_SaveMesJxWorkflowStepDataTimer, m_SaveMesJxWorkflowStepDataTimer_Elapsed, intSync * 10 * 1000);//将【机匣Json】排产下发工序级任务可推送数据写入变更表
            //SyncTimerStatr(ref m_DealJxReportAndStatusDataTimer, m_DealJxReportAndStatusDataTimer_Elapsed, intSync * 10 * 1000);//处理【机匣Json】工序报工和状态数据 add by longb 2025-01-02
            
            //SyncTimerStatr(ref m_ReceiveSaveBusinessIDTimer, m_ReceiveSaveBusinessIDTimer_Elapsed, 10 * 1000);//处理接收到的数据中心任务
            SyncTimerStatr(ref m_SendMESDataToWMSTimer, m_SendMESDataToWMSTimer_Elapsed, 20 * 1000);//推送mes入库数据到数据中心 add zhunan 2025.9.12
            //SyncTimerStatr(ref m_SaveContainerMaterialTimer, m_SaveContainerMaterialTimer_Elapsed, 20 * 1000);//处理WMS推送给MES的发料信息 add zhunan 2025.9.18
            //SyncTimerStatr(ref m_SaveDealFaultBillTimer, m_SaveDealFaultBillTimer_Elapsed, 20 * 1000);//处理WMS推送给MES的返件领取信息 add zhunan 2025.9.22
            //SyncTimerStatr(ref m_DealWMSwayBillFeedBackTimer, m_DealWMSwayBillFeedBackTimer_Elapsed, intSync*20 * 1000);//处理WMS推送给MES的物流信息回传json add longb 20250923
            SyncTimerStatr(ref m_FiveDealFaultBillTimer, m_FiveDealFaultBillTimer_Elapsed, intSync * 20 * 1000);//处理五厂排故返件回传json add longb 20251103
        }

        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync;
            timer.Enabled = true;
            timer.Start();
        }

        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                // LogHelp.WriteLog("WorkingReport定时器启动");
                m_SyncTimer.Enabled = false;
                SendIndustrialNetworkData runMain = new SendIndustrialNetworkData();
                runMain.SendIndustrialNetworkDataSync();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }

            m_SyncTimer.Enabled = true;

        }
        /// <summary>
        /// 与三坐标集成
        /// add:Wangjh 20230505
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ThreeCoordinateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_ThreeCoordinateTimer.Enabled = false;
                ThreeCoordinateCheck runMain = new ThreeCoordinateCheck();
                runMain.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("与三坐标系统集成", myError);
            }

            m_ThreeCoordinateTimer.Enabled = true;

        }

        /// <summary>
        /// 与热表设备采集系统集成
        /// add:Wangjh 20230505
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_RBResCollectionTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_RBResCollectionTimer.Enabled = false;
                RBResourceStatusStepPlan runMain = new RBResourceStatusStepPlan();
                runMain.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("与热表设备采集系统集成", myError);
            }

            m_RBResCollectionTimer.Enabled = true;

        }
        /// <summary>
        /// 与数据中心集成
        /// add by lb 2024-07-11
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_MesToDcTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_MesToDcTimer.Enabled = false;
                pushTableDataToDC push = new pushTableDataToDC();
                push.ExcuteTableDef();//表结构推送
                System.Threading.Thread.Sleep(5000);
                push.Excute();//表数据推送（工艺附件推送）
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("与数据中心集成", myError);
            }

            m_MesToDcTimer.Enabled = true;

        }
        /// <summary>
        /// 与数据中心集成（热表主数据）add by zhangrj 20250728
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_RBMesToDcTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_RBMesToDcTimer.Enabled = false;
                pushTableDataToDC push = new pushTableDataToDC();
                push.ExcuteTableDef();//表结构推送
                System.Threading.Thread.Sleep(5000);
                push.ExcuteRB();//表数据推送（热表主数据）
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("与数据中心集成", myError);
            }
            m_RBMesToDcTimer.Enabled = true;
        }

        #region 热表推送工控网相关 modify by zhangrj 20250715

        /// <summary>
        /// 推送热表转工子卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveRbShipMesDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveRbShipMesDataTimer.Enabled = false;
                SaveRbMESData srm = new SaveRbMESData();
                srm.ExcuteShipContainer();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("热表转工子卡写入变更表出错，原因：", myError);
            }
            m_SaveRbShipMesDataTimer.Enabled = true;
        }
        /// <summary>
        /// 推送热表采购订单批次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveRbOrderMesDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveRbOrderMesDataTimer.Enabled = false;
                SaveRbMESData srm = new SaveRbMESData();
                srm.ExcuteOrderContainer();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("热表采购订单批次写入变更表出错，原因：", myError);
            }
            m_SaveRbOrderMesDataTimer.Enabled = true;
        }
        /// <summary>
        /// 推送热表其他数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveRbOtherMesDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveRbOtherMesDataTimer.Enabled = false;
                SaveRbMESData srm = new SaveRbMESData();
                srm.ExcuteSupplementData();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("热表补充数据写入变更表出错，原因：", myError);
            }
            m_SaveRbOtherMesDataTimer.Enabled = true;
        }

        #endregion

        /// <summary>
        /// 将【件号】流卡可推送数据写入变更表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveMesContainerDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveMesContainerDataTimer.Enabled = false;
                SaveContainerMESData container = new SaveContainerMESData();
                container.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("流卡可推送数据写入变更表出错，原因：", myError);
            }

            m_SaveMesContainerDataTimer.Enabled = true;

        }

        /// <summary>
        /// 将排产下发工序级任务可推送数据写入变更表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveMesWorkflowStepDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveMesWorkflowStepDataTimer.Enabled = false;
                SaveDgMESData dg = new SaveDgMESData();
                dg.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("排产下发工序级任务可推送数据写入变更表出错，原因：", myError);
            }

            m_SaveMesWorkflowStepDataTimer.Enabled = true;

        }

        /// <summary>
        /// 将【机匣Json】排产下发工序级任务可推送数据写入变更表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_SaveMesJxWorkflowStepDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveMesJxWorkflowStepDataTimer.Enabled = false;
                SaveJxMESData jx = new SaveJxMESData();
                jx.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("【机匣Json】排产下发工序级任务可推送数据写入变更表出错，原因：", myError);
            }

            m_SaveMesJxWorkflowStepDataTimer.Enabled = true;

        }
        /// <summary>
        /// 处理【机匣Json】工序报工和状态数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_DealJxReportAndStatusDataTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_DealJxReportAndStatusDataTimer.Enabled = false;
                JxReportAndStatusService jx = new JxReportAndStatusService();
                jx.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【机匣Json】工序报工和状态数据出错，原因：", myError);
            }

            m_DealJxReportAndStatusDataTimer.Enabled = true;

        }

        /// <summary>
        /// 处理接收到的数据中心任务businessid
        /// add:wangjh 20241111
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ReceiveSaveBusinessIDTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_ReceiveSaveBusinessIDTimer.Enabled = false;
                ReceiveBusiness.ReceiveSaveBusiness rs = new ReceiveBusiness.ReceiveSaveBusiness();
                rs.Excute();
            }
            catch (Exception ex)
            {
                rsLog.Error("处理接收到数据中心的任务出错", ex);
            }
            finally
            {
                m_ReceiveSaveBusinessIDTimer.Enabled = true;
            }
        }

        private void m_SendMESDataToWMSTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SendMESDataToWMSTimer.Enabled = false;
                SendMESDataToWMS wms = new SendMESDataToWMS();
                wms.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【MES入库】数据出错，原因：", myError);
            }
            m_SendMESDataToWMSTimer.Enabled = true;
        }

        private void m_SaveContainerMaterialTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveContainerMaterialTimer.Enabled = false;
                SaveContainerMaterialService mes = new SaveContainerMaterialService();
                mes.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【MES发料】数据出错，原因：", myError);
            }
            m_SaveContainerMaterialTimer.Enabled = true;
        }

        private void m_SaveDealFaultBillTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_SaveDealFaultBillTimer.Enabled = false;
                SaveDealFaultBillService mes = new SaveDealFaultBillService();
                mes.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【MES发料】数据出错，原因：", myError);
            }
            m_SaveDealFaultBillTimer.Enabled = true;
        }
        /// <summary>
        /// 处理WMS推送给MES的物流信息回传json add longb 20250923
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_DealWMSwayBillFeedBackTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_DealWMSwayBillFeedBackTimer.Enabled = false;
                DealWMSwayBillFeedBackService mes = new DealWMSwayBillFeedBackService();
                mes.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【物流信息回传】数据出错，原因：", myError);
            }
            m_DealWMSwayBillFeedBackTimer.Enabled = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_FiveDealFaultBillTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_FiveDealFaultBillTimer.Enabled = false;
                SaveFiveDealFaultBillService five = new SaveFiveDealFaultBillService();
                five.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("处理【五厂排故返件】数据出错，原因：", myError);
            }
            m_FiveDealFaultBillTimer.Enabled = true;
        }
        protected override void OnStop()
        {
        }
    }
}
